package com.zcx.qshop.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyType;
import com.zcx.helper.http.note.HttpInlet;
import com.zcx.qshop.entity.Good;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/17.
 */
@HttpInlet(Conn.INLET_JSON_ACTIVITY)
public class JsonActivityAsyGet extends QSAsyGet<JsonActivityAsyGet.Info> {

    public String type, uid;

    public JsonActivityAsyGet(String uid, String type, AsyCallBack<Info> asyCallBack) {

        super(asyCallBack);

        this.uid = uid;

        this.type = type;

        ASY_TYPE = AsyType.MAX_STALE;

    }

    @Override
    public Info parser(JSONObject object) {

        if(object.optString("message").equals("1")){

            Info info = new Info();

            JSONArray array1 = object.optJSONArray("bannerlist");

            if(array1 != null){

                for (int i = 0 ; i < array1.length() ; i ++){

                    JSONObject o = array1.optJSONObject(i);

                    Banner banner = new Banner();

                    banner.info = o.optString("info");

                    banner.picurl = Conn.SERVICE +  o.optString("picurl");

                    info.banners.add(banner);

                }

            }

            JSONArray array2 = object.optJSONArray("goodslist");

            if(array2 != null){

                GoodDouble goodDouble = new GoodDouble();

                for (int i = 0 ; i < array2.length() ; i ++){

                    JSONObject o = array2.optJSONObject(i);

                    Good good = new Good();

                    good.info = o.optString("info");

                    good.picurl = Conn.SERVICE + o.optString("picurl");

                    good.pid = o.optString("id");

                    good.title = o.optString("title");

                    good.isadd = o.optString("isadd");

                    good.price = o.optString("price");

                    good.unit = o.optString("unit");

                    good.number = o.optString("number");

                    good.markprice = o.optString("markprice");

                    if(goodDouble.goodOne == null){

                        goodDouble.goodOne = good;

                        if(i == array2.length() - 1){

                            info.goodDoubles.add(goodDouble);

                        }

                    }else{

                        goodDouble.goodTwo = good;

                        info.goodDoubles.add(goodDouble);

                        goodDouble = new GoodDouble();

                    }

                }

            }

            return info;

        }

        return null;

    }

    public static class Info{

        public List<Banner> banners = new ArrayList<>();

        public List<GoodDouble> goodDoubles = new ArrayList<>();

    }

    public static class Banner implements Serializable{

        public String info, picurl;

    }

    public static class GoodDouble implements Serializable{

        public Good goodOne, goodTwo;

    }

}
